package controllers

import (
	"goblog/models"
	"goblog/models/dao"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/session"
)

type IndexController struct {
	beego.Controller
}

type CategoryVo struct {
	models.Category
	Children []models.Category
}

func SetCatList(session session.Store, data map[interface{}]interface{}) {
	firstCates := session.Get("firstCates")
	if firstCates == nil {
		categoryDao := new(dao.CategoryDao)
		firstCats := categoryDao.FindFirst()
		firstCateVos := []CategoryVo{}
		if len(firstCats) > 0 {
			for _, cat := range firstCats {
				vo := CategoryVo{}
				vo.Id = cat.Id
				vo.Name = cat.Name
				vo.Pid = cat.Pid
				vo.Parent = cat.Parent
				vo.Children = categoryDao.FindChild(cat.Id)
				firstCateVos = append(firstCateVos, vo)
			}
		}
		session.Set("firstCates", firstCateVos)
		data["catList"] = firstCateVos
	} else {
		data["catList"] = firstCates
	}
}

func (this *IndexController) Get() {
	articleDao := new(dao.ArticleDao)
	term := new(dao.ArticleTerm)
	page, _ := this.GetInt("page", 1)
	term.Size = 10
	term.Page = page
	term.Status = dao.DISPLAY
	articleDao.Find(term)
	this.Data["page"] = term
	this.Data["next"] = term.Page + 1
	this.Data["pre"] = term.Page - 1
	seoDao := new(dao.SeoDao)
	seo := seoDao.Get("Home", 0)
	this.Data["seo"] = seo
	this.Layout = "layout/layout.html"
	this.TplName = "index.html"
	SetCatList(this.StartSession(), this.Data)
}
